package utils

import (
	"fmt"
	"gopkg.in/ini.v1"
	"sync"
)

type Settings struct {
	AppMode  string
	HttpPort int
	JwtKey   string

	DB     string
	DBHost string
	DBPort int
	DBUser string
	DBPass string
	DBName string
}

var setting *Settings
var once sync.Once

func GetSettings() *Settings {
	once.Do(func() {
		setting = (&Settings{}).newSettings()
	})
	return setting

}
func (s *Settings) newSettings() *Settings {
	file, err := ini.Load("src/api/config/config.ini")
	if err != nil {
		file, err = ini.Load("../config/config.ini")
		if err != nil {
			fmt.Println("配置文件错误 Error:", err)
			panic("配置文件错误")
		}
	}
	s.loadServer(file)
	s.loadDB(file)
	return s
}
func (s *Settings) loadServer(file *ini.File) {
	s.AppMode = file.Section("server").Key("AppMode").MustString("debug")
	s.HttpPort = file.Section("server").Key("HttpPort").MustInt(8080)
	s.JwtKey = file.Section("server").Key("JwtKey").MustString("admin")
}

func (s *Settings) loadDB(file *ini.File) {
	s.DB = file.Section("database").Key("DB").MustString("mysql")
	s.DBHost = file.Section("database").Key("DBHost").MustString("localhost")
	s.DBPort = file.Section("database").Key("DBPort").MustInt(3306)
	s.DBUser = file.Section("database").Key("DBUser").MustString("root")
	s.DBPass = file.Section("database").Key("DBPass").MustString("hh123456")
	s.DBName = file.Section("database").Key("DBName").MustString("iTunnel")

}
